#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <cstdlib>
#include <map>
#include <ctime>
#include <vector>
#include <queue>
#define debuglog(x) cerr<<"\tdebug:"<<#x<<endl
#define debug(x) cerr<<"\tdebug:"<<#x<<"="<<(x)<<endl
#define debugg(x,y) cerr<<"\tdebug:"<<(x)<<":"<<#y<<"="<<(y)<<endl
#define debugzu(x,a,b) 	cerr<<"\tdebug:"<<#x<<"=\n\t";for(int i=a;i<b;i++)cerr<<x[i]<<" ";fprintf(stderr,"\n");
#define debugerzu(x,a,b,c,d) 	cerr<<"\tdebug:"<<#x<<"=\n\t";for(int i=a;i<b;i++,fprintf(stderr,"\n\t"))for(int j=c;j<d;j++)cerr<<x[i][j]<<" ";fprintf(stderr,"\n");
#define R register
#define L long
#define LL long long
#define I inline
#define U unsigned
#define START clock_t __start=clock();
#define STOP fprintf(stderr,"\n\nUse Time:%fs\n",((double)(clock()-__start)/CLOCKS_PER_SEC));
using namespace std;
I LL read()
{
    R LL x;R bool f;R char c;
    for (f=0; (c=getchar())<'0'||c>'9'; f=c=='-');
    for (x=c-'0'; (c=getchar())>='0'&&c<='9'; x=(x<<3)+(x<<1)+c-'0');
    return f?-x:x;
}
void get_next(char s[],int next[])
{
    int t1=0,t2,lens=strlen(s);
    next[0]=t2=-1;
    while(t1<lens)
        if(t2==-1||s[t1]==s[t2])
            next[++t1]=++t2;
        else
            t2=next[t2];
}
int kmp(char s[],char t[],int next[])
{
    int lens=strlen(s),lent=strlen(t),t1=0,t2=0;
    while(t1<lens)
    {
        if(t2==-1||s[t1]==t[t2])
            t1++,t2++;
        else
            t2=next[t2];
        if(t2==lent)
            return t1-lent;
    }
    return -1;
}
#define MAXN 10000011
char a[MAXN],b[MAXN];
int c[MAXN];

int main()
{
    freopen("7-2.in","r",stdin);
//  freopen("7-2.out","w",stdout);
    for(int t=read();t--;)
    {
        scanf("%s%s",a,b);
        get_next(b,c);
        cout<<kmp(a,b,c)<<endl;
    }
    
    fclose(stdin);
    fclose(stdout);
    return 0;
}